package com.treasury.glasses.service;

import com.treasury.glasses.Main;
import com.treasury.glasses.domain.ActivityLog;
import com.treasury.glasses.domain.ProgramUser;
import com.treasury.glasses.persistence.DataSource;
import java.util.List;

/**
 * Service class for the ActivityController.
 * @author <a href="mailto:glasses@audienz.eu">Jean-Luc Burot</a>
 */

public class ActivityService {
    private final DataSource dataSource = DataSource.getInstance();
    
    /**
     * Gets all the activities to a specific ProgramUser.
     * @param programUser ProgramUser
     * @return List of activities.
     */
    public List<ActivityLog> getActivities(ProgramUser programUser) {
        try {
            return Main.getEntityManager()
                       .createNamedQuery(ActivityLog.ACTIVITIES_OF_USER, ActivityLog.class)
                       .setParameter("programUser", programUser)
                       .getResultList();
        } catch (RuntimeException ex) {
            System.out.println("Database error: " + ex.getMessage());
            return null;
        }
    }
    
    /**
     * Creates an ActivityLog.
     * @param activityLog ActivityLog
     */
    public final void createActivityLog(final ActivityLog activityLog) {
        this.dataSource.save(activityLog);
    }
}
